package org.nowcoder.leetcode.slidingWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * Title  : 3. Longest Substring Without Repeating Characters
 * Source : https://leetcode.cn/problems/longest-substring-without-repeating-characters/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-21
 */

public class LeetCode_3 {
    public int lengthOfLongestSubstring(String s) {
        int left = 0, right = 0;
        int len = 0;
        List<Character> subStr = new ArrayList<>();
        while (right < s.length()) {
            char c = s.charAt(right);
            if (subStr.contains(c)) { //包含重复元素
                len = Math.max(len, subStr.size());
                while (subStr.contains(c)) {
                    subStr.remove(left);
                }
            }
            subStr.add(c);
            right++;
        }
        return Math.max(len, subStr.size());
    }
}
